var redisMemoryInfoInterval;
var rediskeysSizeInterval;
$(document).ready(function () {
    //处理菜单
    var str = "";
    var forTree = function (o) {
        var flag = true;
        for (var i = 0; i < o.length; i++) {
            var urlstr = "";
            if(flag){
                urlstr = $("<li class=\"has-sub active\"></li>");
                flag=false;
            }else{
                urlstr = $("<li class=\"has-sub\"></li>");
            }
            try {
                if (!o[i]["url"]) {
                    urlstr.append('<a href="javascript:;"><b class="caret pull-right"></b><i class="'+o[i]["icon"]+'"></i><span>'+o[i]["text"]+'</span></a>');
                } else {
                    /*if(o[i]["attributes"].num == 10){
                        urlstr.append('<a href="javascript:;" data-id="'+o[i]["id"]+'" name="'+o[i]["url"]+'" onclick="loadMainDialog(this);"><i class="'+o[i]["icon"]+'"></i><span>'+o[i]["text"]+'</span></a>');
                    }else {*/
                        urlstr.append('<a href="javascript:;" data-id="'+o[i]["id"]+'" name="'+o[i]["url"]+'" onclick="loadMain(this);"><i class="'+o[i]["icon"]+'"></i><span>'+o[i]["text"]+'</span></a>');
                    /*}*/
                }
                if (o[i]["children"].length !== 0 && o[i]["attributes"].num == 0) {
                    var child  = o[i]["children"];
                    var subUl = $('<ul class="sub-menu"></ul>');
                    var chil = true;
                    for (var j = 0; j < child.length; j++) {
                        if (chil) {
                            subUl.append('<li class="active"><a href="javascript:;" name="'+child[j]["url"]+'" onclick="loadMain(this);"><i class="'+child[j]["icon"]+'"></i><span>'+child[j]["text"]+'</span></a></li>');
                            chil = false;
                        }else{
                            subUl.append('<li><a href="javascript:;" name="'+child[j]["url"]+'" onclick="loadMain(this);"><i class="'+child[j]["icon"]+'"></i><span>'+child[j]["text"]+'</span></a></li>');
                        }
                    }
                    urlstr.append(subUl);
                }

                $("#menue_nav").append(urlstr);
            } catch (e) {
                console.log(e);
            }
        }
        return str;
    };
    $.post(ctx + "menu/getUserMenu", {"userName": userName}, function (r) {
        if (r.code === 0) {
            var data = r.msg;
            var $crollbarInner = $(".scrollbar-inner");
             forTree(data.children);
            $("#menue_nav").append(' <li><a href="javascript:;" class="sidebar-minify-btn" data-click="sidebar-minify"><i class="fa fa-angle-double-left"></i></a></li>');
            $crollbarInner[0] && $crollbarInner.scrollbar().scrollLock();
            App.init();
            $("[data-toggle='tooltip']").tooltip();
        } else {
            $JuXue.n_danger("系统忙碌,刷新后再试");
        }
    })
});
/**修改个人信息**/
function modifyUserInfo() {
    $.post(ctx + "user/profile", function (r) {$("#index_user_info").empty().append(r).modal("show")});
}
/**直接弹框**/
function loadMainDialog (obj) {
    var $data_id = $(obj).attr("data-id");
    $("#menu-id").val($data_id);
    /**清楚li 的active 属性**/
    $("#menue_nav").find("li").each(function(){$(this).removeClass("active");});
    $(obj).parent().addClass("active");
    // 加载内容
    var $name = $(obj).attr("name");
    $.post(ctx + $name, {}, function (r) {
        if (r.code === 401) {
            $JuXue.n_danger("登录已失效，您的账号已被踢出或已在别的地方登录，请重新登录。如果密码遭到泄露，请立即修改密码！");
            setTimeout(function () {
                location.href = ctx + "login"
            }, 4000);
            return;
        } else if (r.code === 500) {
            $JuXue.n_danger("系统忙碌,刷新后再试");return;
        }
        $("#sale_help_index").empty().append(r).modal('show').draggable();
    });
}

/**
 * 用于加载菜单内容
 * @param obj
 */
function loadMain(obj) {
    var $data_id = $(obj).attr("data-id");
    $("#menu-id").val($data_id);
    /**清楚li 的active 属性**/
    $("#menue_nav").find("li").each(function(){$(this).removeClass("active");});
    $(obj).parent().addClass("active");
    // 加载内容
    var $name = $(obj).attr("name");
    $.post(ctx + $name, {}, function (r) {
        if (r.code === 401) {
            $JuXue.n_danger("登录已失效，您的账号已被踢出或已在别的地方登录，请重新登录。如果密码遭到泄露，请立即修改密码！");
            setTimeout(function () {
                location.href = ctx + "login"
            }, 4000);
            return;
        } else if (r.code === 500) {
          $JuXue.n_danger("系统忙碌,刷新后再试");return;
        }
        clearInterval(rediskeysSizeInterval);
        clearInterval(redisMemoryInfoInterval);
        $("#ajax-content").html("").append(r);
    });
}

